import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { UserService } from 'src/app/service/user.service';
import { YearawardService } from 'src/app/module/yearaward/yearaward.service';
import { BaseService } from 'src/app/service/base.service';
import { UtilsService } from 'src/app/service/utils.service';

@Component({
  selector: 'app-luckymomend',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss', '../../commonList.scss'],
})
export class LuckymomendComponent implements OnInit, OnDestroy {
  @Input() timeConfig: any[] = [];
  /* ___ _ ___ */
  public deployUrl: string; /* 资源地址前缀 */
  /* ___ _ ___ */
  public flag = false;
  public isPC = false; /* 是否pc */
  public isAndroid = false; /* 是否安卓 */
  public onRefrash: any; /* 刷新中 */
  public pageSize = 30; /* 每页条数 */
  public stageId: number; //请求数据ID
  public haveOver: boolean = false; //是否已经结榜
  public reshing: any = null;
  /* ___ _ ___ */
  public top3List: EndYear.IAnchor[] = []; /* TOP3 */
  public othersList: EndYear.IAnchor[] = []; /* 危险区 */
  public safetyList: EndYear.IAnchor[] = []; /* 安全区 */
  public dangersList: EndYear.IAnchor[] = []; /* 淘汰区 */
  public currentAnchorInfo: EndYear.ICurrentAnchor = {
    nickname: '',
    avatarUrl: '',
    score: 0,
    rankDesc: '-',
    rank: 0,
    isMysteryMan: false,
  }; /* 当前主播信息 */

  constructor(
    private userService: UserService,
    private yearawardService: YearawardService,
    private baseService: BaseService,
    private utilsservice: UtilsService
  ) {}

  ngOnInit(): void {
    // 获取
    this.deployUrl = this.userService.getDeployUrl();
    this.isPC = this.userService.getIsPc();
    this.isAndroid = this.utilsservice.getBrowserVersion();
    this.initPage();
    // 组件刷新通信
    this.reshing = this.utilsservice.MsgSub.subscribe((res: any) => {
      this.initPage();
    });
  }
  ngOnDestroy() {
    if (this.reshing) {
      this.reshing.unsubscribe();
    }
  }
  public initPage() {
    this.stageId = this.timeConfig.find((itm) => itm.type === 18).id;
    if (
      new Date().getTime() >
      this.timeConfig.find((itm) => itm.type === 18).endTime * 1000
    ) {
      this.haveOver = true;
    } else {
      this.haveOver = false;
    }
    // 请求数据
    this.getList();
    this.getCurrentAnchor();
  }
  /**
   * @description: 初始化请求列表
   */
  // 获取榜单
public getList() {
  return new Promise((r, j) => {
    this.onRefrash = true;
    this.yearawardService.getActivityRanking(
      (res) => {
        this.onRefrash = false;
        if (res.code === 200) {
          const len = 3 - res.data.list.length;
          if (len > 0) {
            for (let i = 0; i < len; i++) {
              res.data.list.push({
                avatarUrl: '',
                score: 0,
                rank: i + 1,
                nickname: '虚位以待',
                rankDesc: '-',
              });
            }
          }
          this.top3List = res.data.list.slice(0, 3) || [];
          this.othersList = res.data.list.slice(3) || [];
          r(res.data);
        } else {
          this.baseService.showToast(res.message, 1500);
          j();
        }
      },
      this.stageId,
      this.pageSize,
      (err) => {
        this.onRefrash = false;
      }
    );
  });
}

/**
 * @description: 获取当前主播
 */
getCurrentAnchor() {
  const ob = this.yearawardService.getCurrentAnchorRanking(
    (res: any) => {
      if (res.code === 200) {
        this.currentAnchorInfo = res.data;
      } else {
        this.baseService.showToast(res.message, 1500);
      }
    },
    this.baseService.getCurrentId(),
    this.stageId,
    (err: any) => {}
  );
}
  // 跳转直播间
  public gotoRoom(item: any, type: any) {
    if (
      (type === 'anchor' && !item.uid) ||
      (type === 'user' && !item.userUid)
    ) {
      return;
    }
    const obj = { ...item };
    if (type === 'anchor') {
      obj.isMysteryMan = false;
    }
    obj.livImage = item.avatarUrl;
    this.utilsservice.gotoRoom(obj, type);
  }
}
